﻿
/*
作者：王伟 
作用：删除分类

declare @str nvarchar(100)

exec Clear_class 10,@str output
print(@str)

DeleteClass 5
*/
CREATE procedure DeleteClass
(
@ClassID int=0,
@ErrInfo nvarchar(100)='' output
)
As

if(@ClassID=0)
begin
	Set @ErrInfo='参数传递不足'
	return 	
end

if not exists(Select ClassID,RootID,Depth,ParentID,Child,PrevID,NextID  From ArticleClass Where ClassID=@ClassID)
begin
	--print('d')
	Set @ErrInfo='栏目不存在，或者已经被删除'
	return 
end

declare @RsChild int 
declare @PrevID int 
declare @NextID int 
declare @Depth int 
declare @ParentID int 


Select @Depth=Depth,@ParentID=ParentID,@RsChild=Child,@PrevID=PrevID,@NextID=NextID  From ArticleClass Where ClassID=@ClassID

if @RsChild>0 
begin
	--print('d')
	Set @ErrInfo='该栏目含有子栏目，请删除其子栏目后再进行删除本栏目的操作'
	return 
	
end 

if(@Depth>0)
begin
	update ArticleClass set child=child-1  where ClassID=@ParentID
end 
	delete  From ArticleClass Where ClassID=@ClassID
	if @PrevID>0 
	begin
		update ArticleClass set NextID=@NextID where ClassID=@PrevID
	end 
	if @NextID>0 
	begin
		update ArticleClass set PrevID=@PrevID where ClassID=@NextID
	end 
	Set @ErrInfo='成功'
	return 
GO
